package com.example.demo.common.aop;

import com.example.demo.common.anotation.CheckUserType;
import com.example.demo.common.enums.SystemEnum;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.thymeleaf.expression.Maps;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Aspect
@Component
@Slf4j
public class UserCheckCall {

    public static final String FUCK_LOG_TIME = "execution_time";

    // 定义一个 ThreadLocal 变量
    private static final ThreadLocal<Map<String, Object>> localMap = new ThreadLocal<>();

    public static void setLocalMap(String key, Object value) {
        Map<String, Object> v1 = localMap.get();
        if(CollectionUtils.isEmpty(v1)){
            v1 = new HashMap<>();
        }
        v1.put(key,value);
        localMap.set(v1);
    }

    public static Map<String, Object> getLocalMap() {
        if (CollectionUtils.isEmpty(localMap.get())) {
            return null;
        }
        return localMap.get();
    }

    public static void deleteLocalMap() {
        localMap.remove();
    }



    @Before("@annotation(com.example.demo.common.anotation.CheckUserType)")
    public void beforeMeth(JoinPoint joinPoint) {
        long timeMillis = System.currentTimeMillis();
        setLocalMap(FUCK_LOG_TIME, timeMillis);
        log.info("before method running，start time--------------{}",timeMillis);
        log.info("start method name1--------------{}",joinPoint.getSignature().getName());
    }

    @AfterReturning("@annotation(com.example.demo.common.anotation.CheckUserType)")
    public void afterMeth(JoinPoint joinPoint) {
        long currentTimeMillis = System.currentTimeMillis();
        long questTime = (long) getLocalMap().get(FUCK_LOG_TIME);
        setLocalMap(FUCK_LOG_TIME, currentTimeMillis-questTime);
        log.info("after return method running，start time--------------{}",currentTimeMillis);
        log.info("start method name2--------------{}",joinPoint.getSignature().getName());
    }


    @AfterThrowing("@annotation(com.example.demo.common.anotation.CheckUserType)")
    public void afterThrowing(JoinPoint joinPoint) {
        long currentTimeMillis = System.currentTimeMillis();
        log.info("AfterThrowing method running，start time--------------{}",currentTimeMillis);
        log.info("start method name3--------------{}",joinPoint.getSignature().getName());
    }

    @After("@annotation(com.example.demo.common.anotation.CheckUserType)")
    public void after(JoinPoint joinPoint) {
        long currentTimeMillis = System.currentTimeMillis();
        log.info("After method running，start time--------------{}",currentTimeMillis);
        log.info("接口耗时:{}", getLocalMap().get(FUCK_LOG_TIME));
        log.info("start method name4--------------{}",joinPoint.getSignature().getName());
        deleteLocalMap();
    }




}
